var app = getApp();
Page({
    data:{
        picSrc:'',
        words:'',
        sign:1,
        totalNum:0
    },
    backHome:function(){
        wx.redirectTo({
          url: '/pages/home/home',
        })
    },
    onReady:function(e){
        var self=this
        var value = wx.getStorageSync('pic');
        // app.showToast()
        this.setData({
            picSrc:value
        })
        // console.log(value[0])
        wx.uploadFile({
        url:"https://www.hibigpc.com/springUpload",
            filePath: value[0],
            name:'file',
            success(res) {
                wx.setStorageSync('pic2', res.data)
            },
            fail:function(res){
                // console.log()
                wx.showModal({
                    title: '提示',
                    content:res.errMsg,
                    showCancel:false
                })
            }
        })
    },
    textInput:function(e){
        var txt=e.detail.value
        var len=txt.length
        wx.setStorageSync('content', txt)
        this.setData({
            words:txt,
            totalNum:len
        })
        
    },
    choose:function(e){
        var that=this
        var signIndex=e.target.dataset.zsign
        that.setData({
            sign:signIndex
        })
    },
    sendWord:function(){
        var value = wx.getStorageSync('userInfo')
        var img=wx.getStorageSync('pic2')
        var brand=wx.getStorageSync('brand')
        var content=this.data.words
        var len=content.replace(/[^\x00-\xff]/g, '__').length
        if(len>139){
            wx.showModal({
                title: '提示',
                content:'输入内容已超出最大字数',
                showCancel:false
            })
            return
        }
        var self=this
        // app.showToast()
        wx.request({
            url: 'https://www.hibigpc.com/saveWords',
            method:'post',
            data: {
                uuid:value.uuid,
                word:content,
                img:img,
                idSign:this.data.sign,
                brandId:brand.id

            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if(res.data.status=='0'){
                    if(self.data.sign==1){
                        wx.redirectTo({
                            url: '/pages/home/home',
                        })
                        wx.removeStorageSync('newStroList')
                        wx.removeStorageSync('hotStroList')
                    }else{
                        wx.removeStorageSync('newStroList')
                        wx.removeStorageSync('hotStroList')
                        wx.showModal({
                            title: '提示',
                            content:'发表成功',
                            showCancel:false,
                            success: function(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '/pages/home/home',
                                })
                            } 
                            }
                        })
                        
                    }
                }
                
            }
        })
    },
    onShareAppMessage: function () {
        var brandId=wx.getStorageSync('brandId')
        return {
        title: '到此一玩小程序',
        path: '/pages/home/home?brandId='+brandId,
        success: function(res) {
            // 分享成功
        },
        fail: function(res) {
            // 分享失败
        }
        }
    }
})